<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/10
 * Time: 17:26
 */

namespace App\Services;

use App\Models\Role;

class RoleService extends BaseService
{
    protected $modelClass = Role::class;

    protected $permissionService;

    public function __construct(PermissionService $permissionService)
    {
        $this->permissionService = $permissionService;

        parent::__construct();
    }

    /**
     * 新增
     * @param $postData
     * @return bool|string
     * @throws \Exception
     */
    public function add($postData)
    {
        try {
            \DB::beginTransaction();

            $postData['uuid'] = uuid();
            $obj = $this->saveData($postData);

            $permArr = $this->permissionService->model()->whereIn('uuid', $postData['access'])->pluck('perm_id')->toArray();
            $obj->attachPerm($permArr);

            \DB::commit();

            return true;
        } catch (\Exception $e) {
            \DB::rollBack();
            throw new \Exception('新增失败！');
        }
    }

    /**
     * 编辑
     * @param $uuid
     * @param $postData
     * @return bool|string
     * @throws \Exception
     */
    public function edit($uuid, $postData)
    {
        try {
            \DB::beginTransaction();

            $obj = $this->detail($uuid);
            if (!$obj) {
                throw new \Exception('未找到！');
            }

            if ($obj->name == 'root') {
                throw new \Exception('禁止修改root！');
            }

            if ($this->model()->where('name', $postData['name'])->pluck('uuid')->diff([$uuid])->count() > 0) {
                throw new \Exception('角色名称已存在！');
            }

            $obj->update($postData);

            $permArr = $this->permissionService->model()->whereIn('uuid', $postData['access'])->pluck('perm_id')->toArray();
            $obj->syncPerm($permArr);

            \DB::commit();

            return true;
        } catch (\Exception $e) {
            \DB::rollBack();
            throw new \Exception('编辑失败！');
        }
    }

    /**
     * 删除
     * @param $uuid
     * @return mixed
     * @throws \Exception
     */
    public function remove($uuid)
    {
        $obj = $this->detail($uuid);
        if (empty($obj)) {
            throw new \Exception('未找到！');
        }

        if ($obj->name == 'root') {
            throw new \Exception('禁止删除root！');
        }

        if (count($obj->users) > 0) {
            throw new \Exception('此角色有绑定的用户，禁止删除！');
        }

        if (!$obj->update(['is_enable' => 0])) {
            throw new \Exception('删除失败！');
        }

        return true;
    }

    /**
     * 获取详情
     * @param $uuid
     * @return mixed
     * @throws \Exception
     */
    public function detail($uuid)
    {
        return $this->model()->where('uuid', $uuid)->with(['users', 'perms'])->first();
    }

    /**
     * @param array $search
     * @param int $per_page
     * @return array
     */
    public function getList($search=[], $per_page=20)
    {
        $columns = ['name', 'display_name', 'description', 'created_at'];

        $model = $this->searcher($this->model, $search)->withCount(['perms', 'users']);

        $conf = [
            'title' => '角色列表',
            'search' => 'conf_search.role_getList',
            'columns' => 'conf_columns.role_getList',
            'buttons' => 'conf_buttons.role_getList'
        ];
        return $this->get_paginate($model->paginate($per_page, $columns), $conf);
    }
}